package com.team.abs.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;



import com.team.abs.dao.taskinsert;
import com.team.abs.dao.taskinsertImpl;
import com.team.abs.service.uploadService;

@Controller
@RequestMapping("/pop")
public class uploadcontroller {
	@Autowired 
	private uploadService uploadServiceImp;
	
	//后端处理上传数据并加载到数据库
	@Autowired
	private  taskinsertImpl task;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	
	@RequestMapping(value="/upload", method=RequestMethod.POST)
    public  ResponseEntity<String> uploadImage(HttpServletRequest request,
			@RequestParam MultipartFile uploadfile,
			@RequestParam(value="taskname",defaultValue="")String taskname,
			@RequestParam(value="user_id",defaultValue="0") int user_id) throws IOException {
      
		System.out.println(taskname);
        
        Map<String,Object> map=new HashMap<>();
		map.put("task_name",taskname);
		map.put("id",Integer.valueOf(user_id));
		map.put("task_time", new Timestamp(new Date().getTime()));
		task.insert_taskname_get_task_id(map);
		task.insert_task_id_user_id((int)map.get("id"),user_id);
		
	    String resultUrl;
	       // System.out.println("done");
	    byte[] bytes = uploadfile.getBytes();
	    resultUrl = uploadServiceImp.dataTosql(String.valueOf(map.get("id")),bytes);
	    //存储源数据
	    List<String>list=bytetolist(bytes);
	    //存储分词后结果
	    List<String>clearlist=bytetolist("D:\\textfile\\"+String.valueOf(map.get("id"))+"_jieba.txt");
	    //将源数据放入reids中
	    stringRedisTemplate.opsForList().rightPushAll(String.valueOf(map.get("id")), list);
	    //将分词结果放入redis中
	    stringRedisTemplate.opsForList().rightPushAll(String.valueOf(map.get("id"))+"_jieba", clearlist);
        return ResponseEntity.ok(resultUrl);
    }
	
	private List<String> bytetolist(String path) {
		
		  File file = new File(path);
		    BufferedReader reader = null;
		    StringBuffer sbf = new StringBuffer();
		    List<String>ans=new ArrayList();
		    try {
		        reader = new BufferedReader(new FileReader(file));
		        String tempStr;
		        while ((tempStr = reader.readLine()) != null) {
		        	ans.add(tempStr);
		        }
		        reader.close();
		        return ans;
		    } catch (IOException e) {
		        e.printStackTrace();
		    } finally {
		        if (reader != null) {
		            try {
		                reader.close();
		            } catch (IOException e1) {
		                e1.printStackTrace();
		            }
		        }
		    }
		    return ans;
	}

	public List<String>bytetolist(byte[]bytes){
		
		List<String>list=new ArrayList();
		String st=new String(bytes);
		for(String temp:st.split("\\r\\n"))
			if(temp.length()>0) {
			list.add(temp);
		//System.out.println(temp);
			}
		return list;
	}
}
